package GUI.lecturerGUI;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.JTextArea;

import common.Perror;

import GUI.MainGUI;

import controllers.LecturerController;
import entities.Subject;

@SuppressWarnings("serial")
public class LecturerQuestions extends JPanel implements ActionListener,
		FocusListener, KeyListener {

	private ArrayList<Subject> subjectLst;
	private LecturerController lecturerCtrl;

	private JTextField txtQuestionNumber;

	private JTextArea txtCorrectAnswer;
	private JTextArea txtIncorrectNumber1;
	private JTextArea txtIncorrectNumber2;
	private JTextArea txtIncorrectNumber3;
	private JTextArea txtQuestion;
	private JTextArea txtInstruction;

	private JLabel lblEnterCorrectAnswer;
	private JLabel lblQuestionCourse;
	private JLabel lblEnterIncorrect;
	private JLabel lblQuestionNum;
	private JLabel lblQuestionInstruction;
	private JLabel lblQuestion;

	private JComboBox cboxSubjectNumber;// TODO JComboBox<String>

	private JButton btnExit;
	private JButton btnMainMenu;
	private JButton btnGenerateQuestion;

	/**
	 * 
	 * @param lecturerCtrl
	 */

	public LecturerQuestions(LecturerController lecturerCtrl) {

		super();
		this.lecturerCtrl = lecturerCtrl;
		initialize();
	}

	private void initialize() {
		setBorder(new TitledBorder(null, "Lecturer  Question: ",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);
		setLayout(null);

		add(getSubjectNumber());
		add(GETlblEnterCorrectAnswer());
		add(GETlblQuestionCours());
		add(GETtxtCorrectAnswer());
		add(GETlblEnterIncorrect());
		add(GETtxtIncorrecrNumber1());
		add(GETtxtIncorrecrNumber2());
		add(GETtxtIncorrecrNumber3());
		add(GETtxtQuestionNumber());
		add(GETlblQuestionNum());
		add(GETbtnExit());
		add(GETbtnMainMenu());
		add(GETbtnGenerateQuestion());
		add(GETlblQuestion());
		add(getTxtInstruction());
		add(GETQuestion());
		add(getLblQuestionInstruction());
	}

	private JComboBox getSubjectNumber() {// TODO JComboBox<String>

		if (cboxSubjectNumber == null) {
			cboxSubjectNumber = new JComboBox();// TODO JComboBox<String>
			cboxSubjectNumber.setMaximumRowCount(20);
			cboxSubjectNumber.setBounds(12, 44, 175, 20);

		}
		return cboxSubjectNumber;
	}

	private JLabel GETlblEnterCorrectAnswer() {
		if (lblEnterCorrectAnswer == null) {
			lblEnterCorrectAnswer = new JLabel("Correct answer:");
			lblEnterCorrectAnswer.setBounds(27, 211, 124, 14);
		}
		return lblEnterCorrectAnswer;
	}

	private JLabel GETlblQuestionCours() {
		if (lblQuestionCourse == null) {
			lblQuestionCourse = new JLabel("Course: ");
			lblQuestionCourse.setBounds(24, 22, 77, 14);
		}
		return lblQuestionCourse;
	}

	private JTextArea GETtxtCorrectAnswer() {
		if (txtCorrectAnswer == null) {
			txtCorrectAnswer = new JTextArea();
			txtCorrectAnswer.setEnabled(false);
			txtCorrectAnswer.setText("Enter correct answer");
			txtCorrectAnswer.setBounds(24, 236, 415, 31);
			txtCorrectAnswer.addFocusListener(this);
			txtCorrectAnswer.addKeyListener(this);
		}
		return txtCorrectAnswer;
	}

	private JLabel GETlblEnterIncorrect() {
		if (lblEnterIncorrect == null) {
			lblEnterIncorrect = new JLabel("Incorrect answers:");
			lblEnterIncorrect.setBounds(26, 276, 161, 14);
		}
		return lblEnterIncorrect;
	}

	private JTextArea GETtxtIncorrecrNumber1() {
		if (txtIncorrectNumber1 == null) {
			txtIncorrectNumber1 = new JTextArea();
			txtIncorrectNumber1.setEnabled(false);
			txtIncorrectNumber1.setText("Enter incorrect number 1");
			txtIncorrectNumber1.setBounds(24, 301, 415, 31);
			txtIncorrectNumber1.addFocusListener(this);
			txtIncorrectNumber1.addKeyListener(this);
		}
		return txtIncorrectNumber1;
	}

	private JTextArea GETtxtIncorrecrNumber2() {
		if (txtIncorrectNumber2 == null) {
			txtIncorrectNumber2 = new JTextArea();
			txtIncorrectNumber2.setEnabled(false);
			txtIncorrectNumber2.setText("Enter incorrect number 2");
			txtIncorrectNumber2.setBounds(24, 337, 415, 31);
			txtIncorrectNumber2.addFocusListener(this);
			txtIncorrectNumber2.addKeyListener(this);
		}
		return txtIncorrectNumber2;

	}

	private JTextArea GETtxtIncorrecrNumber3() {
		if (txtIncorrectNumber3 == null) {
			txtIncorrectNumber3 = new JTextArea();
			txtIncorrectNumber3.setEnabled(false);
			txtIncorrectNumber3.setText("Enter incorrect number 3");
			txtIncorrectNumber3.setBounds(24, 379, 415, 31);
			txtIncorrectNumber3.addFocusListener(this);
			txtIncorrectNumber3.addKeyListener(this);
		}
		return txtIncorrectNumber3;
	}

	private JTextField GETtxtQuestionNumber() {
		if (txtQuestionNumber == null) {
			txtQuestionNumber = new JTextField();
			txtQuestionNumber.setText("0");
			txtQuestionNumber.setEnabled(false);
			txtQuestionNumber.setColumns(10);
			txtQuestionNumber.setBounds(268, 44, 173, 20);
		}
		return txtQuestionNumber;
	}

	private JLabel GETlblQuestionNum() {
		if (lblQuestionNum == null) {
			lblQuestionNum = new JLabel("Question Number: ");
			lblQuestionNum.setBounds(268, 22, 131, 14);

		}
		return lblQuestionNum;
	}

	private JButton GETbtnExit() {
		if (btnExit == null) {
			btnExit = new JButton("EXIT");
			btnExit.setBounds(380, 443, 110, 48);
			btnExit.addActionListener(this);

		}
		return btnExit;
	}

	private JButton GETbtnMainMenu() {
		if (btnMainMenu == null) {
			btnMainMenu = new JButton("Main menu");
			btnMainMenu.setBounds(10, 442, 96, 50);
			btnMainMenu.addActionListener(this);
		}
		return btnMainMenu;
	}

	private JButton GETbtnGenerateQuestion() {
		if (btnGenerateQuestion == null) {
			btnGenerateQuestion = new JButton("Generate question");
			btnGenerateQuestion.setEnabled(false);
			btnGenerateQuestion.setBounds(145, 442, 200, 50);
			btnGenerateQuestion.addActionListener(this);
		}
		return btnGenerateQuestion;
	}

	/**
	 * clean Lecturer Question GUI
	 */
	public void cleanLecturerQuestionGUI() {
		cboxSubjectNumber.setSelectedIndex(0);
		txtQuestion.setText("Enter Question");
		txtInstruction.setText("(optinal) Add question instrutcions");
		txtCorrectAnswer.setText("Enter correct answer");
		txtIncorrectNumber1.setText("Enter incorrect number 1");
		txtIncorrectNumber2.setText("Enter incorrect number 2");
		txtIncorrectNumber3.setText("Enter incorrect number 3");
		btnGenerateQuestion.setEnabled(false);

	}

	/**
	 * enable Generate Question button
	 */
	public void enableGenerateQuestion() {
		if (!(txtQuestion.getText().equals("Enter Question")
				|| txtCorrectAnswer.getText().equals("Enter correct answer")
				|| txtIncorrectNumber1.getText().equals(
						"Enter incorrect number 1")
				|| txtIncorrectNumber2.getText().equals(
						"Enter incorrect number 2")
				|| txtIncorrectNumber3.getText().equals(
						"Enter incorrect number 3")
				|| txtQuestion.getText().equals("")
				|| txtCorrectAnswer.getText().equals("")
				|| txtIncorrectNumber1.getText().equals("")
				|| txtIncorrectNumber2.getText().equals("") 
				|| txtIncorrectNumber3.getText().equals("")))
			btnGenerateQuestion.setEnabled(true);
		else
			btnGenerateQuestion.setEnabled(false);
	}

	/**
	 * 
	 * @return the Question from the text box
	 */
	private JTextArea GETQuestion() {
		if (txtQuestion == null) {
			txtQuestion = new JTextArea();
			txtQuestion.setText("Enter Question");
			txtQuestion.setEnabled(false);
			txtQuestion.setBounds(22, 103, 415, 31);
			txtQuestion.addKeyListener(this);
			txtQuestion.addFocusListener(this);
		}
		return txtQuestion;
	}

	private JLabel GETlblQuestion() {
		if (lblQuestion == null) {
			lblQuestion = new JLabel("Question:");
			lblQuestion.setBounds(25, 75, 103, 14);

		}
		return lblQuestion;
	}

	private JLabel getLblQuestionInstruction() {
		if (lblQuestionInstruction == null) {
			lblQuestionInstruction = new JLabel("Question instruction:");
			lblQuestionInstruction.setBounds(27, 145, 124, 14);
		}
		return lblQuestionInstruction;
	}

	private JTextArea getTxtInstruction() {
		if (txtInstruction == null) {
			txtInstruction = new JTextArea();
			txtInstruction.setText("(optinal) Add question instrutcions");
			txtInstruction.setEnabled(false);
			txtInstruction.setBounds(24, 170, 415, 31);
			txtInstruction.addFocusListener(this);
		}
		return txtInstruction;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == cboxSubjectNumber) {
			if ((subjectLst != null) || (!subjectLst.isEmpty())) {
				Integer qnum;
				int comboindex = cboxSubjectNumber.getSelectedIndex();
				if ((comboindex < subjectLst.size()) && (comboindex >= 0)) {
					Subject selectedSubject = subjectLst.get(comboindex);
					int cID = selectedSubject.getSubjectID();
					qnum = new Integer(lecturerCtrl.getNewQuestionID(cID));
					txtQuestionNumber.setText(qnum.toString());
					enableTxts();
				}
			}
		}

		if (e.getSource() == btnMainMenu) {
			lecturerCtrl.handleLectutereGUI(LecturerController.MAIN);
			cleanLecturerQuestionGUI();
			cboxSubjectNumber.removeAllItems();
		}
		if (e.getSource() == btnExit) {
			lecturerCtrl.handleLectutereGUI(LecturerController.EXIT);
		}
		if (e.getSource() == btnGenerateQuestion) {
			lecturerCtrl.CreateNewQuestion();
		}

	}

	/**
	 * enable all Texts filed
	 */
	private void enableTxts() {
		txtQuestion.setEnabled(true);
		txtInstruction.setEnabled(true);
		txtCorrectAnswer.setEnabled(true);
		txtIncorrectNumber1.setEnabled(true);
		txtIncorrectNumber2.setEnabled(true);
		txtIncorrectNumber3.setEnabled(true);

	}

	/**
	 * 
	 * @return the selected subject from combo
	 */
	public Integer getSubjectID() {
		Integer temp = new Integer(subjectLst.get(
				(cboxSubjectNumber.getSelectedIndex())).getSubjectID());
		return temp;
	}

	public String getAnswerC() {
		return txtIncorrectNumber3.getText().replaceAll("'", "");
	}

	public Integer getID() {
		return Integer.parseInt(txtQuestionNumber.getText());
	}

	public String getAnswerB() {
		
		return txtIncorrectNumber2.getText().replaceAll("'", "");
	}

	public String getAnswerA() {

		return txtIncorrectNumber1.getText().replaceAll("'", "");
	}

	public String getCorrectAnswer() {

		return txtCorrectAnswer.getText().replaceAll("'", "");
	}

	public String getInsructions() {
		if (!txtInstruction.equals("(optinal) Add question instrutcions"))
			return txtInstruction.getText().replaceAll("'", "");
		return " ";
	}

	public String getQuestionText() {
		return txtQuestion.getText().replaceAll("'", "");
	}

	/**
	 * 
	 * @param sbj
	 *            the subject to add the combo
	 */
	public void addSubjects(ArrayList<Subject> sbj) {
		this.subjectLst = new ArrayList<Subject>();
		this.subjectLst.addAll(0, sbj);
		for (int i = 0; i < (sbj.size()); i++)
			cboxSubjectNumber.addItem(subjectLst.get(i).getSubjectID() + " : "
					+ sbj.get(i).getSubjectName());
		cboxSubjectNumber.addActionListener(this);
	}

	public void focusGained(FocusEvent e) {
		if (e.getSource() == txtCorrectAnswer
				&& txtCorrectAnswer.getText().equals("Enter correct answer"))
			txtCorrectAnswer.setText("");
		if (e.getSource() == txtIncorrectNumber1
				&& txtIncorrectNumber1.getText().equals(
						"Enter incorrect number 1"))
			txtIncorrectNumber1.setText("");
		if (e.getSource() == txtIncorrectNumber2
				&& txtIncorrectNumber2.getText().equals(
						"Enter incorrect number 2"))
			txtIncorrectNumber2.setText("");
		if (e.getSource() == txtIncorrectNumber3
				&& txtIncorrectNumber3.getText().equals(
						"Enter incorrect number 3"))
			txtIncorrectNumber3.setText("");
		if (e.getSource() == txtQuestion
				&& txtQuestion.getText().equals("Enter Question"))
			txtQuestion.setText("");
		if (e.getSource() == txtInstruction
				&& txtInstruction.getText().equals(
						"(optinal) Add question instrutcions"))
			txtInstruction.setText("");
	}

	@Override
	public void focusLost(FocusEvent e) {
		if (e.getSource() == txtCorrectAnswer
				&& txtCorrectAnswer.getText().equals(""))
			txtCorrectAnswer.setText("Enter correct answer");
		if (e.getSource() == txtIncorrectNumber1
				&& txtIncorrectNumber1.getText().equals(""))
			txtIncorrectNumber1.setText("Enter incorrect number 1");
		if (e.getSource() == txtIncorrectNumber2
				&& txtIncorrectNumber2.getText().equals(""))
			txtIncorrectNumber2.setText("Enter incorrect number 2");
		if (e.getSource() == txtIncorrectNumber3
				&& txtIncorrectNumber3.getText().equals(""))
			txtIncorrectNumber3.setText("Enter incorrect number 3");
		if (e.getSource() == txtQuestion && txtQuestion.getText().equals(""))
			txtQuestion.setText("Enter Question");
		if (e.getSource() == txtInstruction
				&& txtInstruction.getText().equals(""))
			txtInstruction.setText("(optinal) Add question instrutcions");

	}

	@Override
	public void keyPressed(KeyEvent key) {
		if (key.getKeyChar() == '\'')
			Perror.pError("please do not use the charecter ' ");
		
			
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		enableGenerateQuestion();

	}

	@Override
	public void keyTyped(KeyEvent arg0) {

	}

}